package sein.android.hopenote;

import java.util.ArrayList;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class Note_Add_Adapter extends BaseAdapter {
	Context context = null;
	int layoutId = 0;
	ArrayList<ItemData> ResultDataArr = null;
	LayoutInflater Inflater = null;

	Note_Add_Adapter(Context _context, int _layoutId,
			ArrayList<ItemData> _ResultDataArr) {
		context = _context;
		layoutId = _layoutId;
		ResultDataArr = _ResultDataArr;
		Inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return (ResultDataArr == null) ? 0 : ResultDataArr.size();
	}

	public Object getItem(int position) {
		return (getCount() > position) ? ResultDataArr.get(position) : null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		if (convertView == null) {
			convertView = Inflater.inflate(layoutId, parent, false);
		}

		TextView tv_item = (TextView) convertView.findViewById(R.id.tv_item);
		tv_item.setText(ResultDataArr.get(position).item_name);
		// ResultDataArr.get(position).et_document1 =
		// (EditText)convertView.findViewById(R.id.et_document1);
		// ResultDataArr.get(position).et_document2 =
		// (EditText)convertView.findViewById(R.id.et_document2);
		ResultDataArr.get(position).et_document1 = (EditText) convertView
				.findViewById(R.id.et_document1);
		ResultDataArr.get(position).et_document2 = (EditText) convertView
				.findViewById(R.id.et_document2);
		ResultDataArr.get(position).et_document1.setText(ResultDataArr.get(position).document1);
		ResultDataArr.get(position).et_document2.setText(ResultDataArr.get(position).document2);
		TextView tv_unit = (TextView) convertView.findViewById(R.id.tv_unit);
		tv_unit.setText(ResultDataArr.get(position).item_unit);
		Log.i("noteadd", "count" + ResultDataArr.get(position).document_count);
		ResultDataArr.get(position).et_document1.setVisibility(View.VISIBLE);
		// et_document1.setNextFocusDownId(et_document2.getId());
		ResultDataArr.get(position).et_document2.setVisibility(View.VISIBLE);
		if (ResultDataArr.get(position).document_count.equals("1")) {
			// et_document1.setNextFocusDownId(0);
			ResultDataArr.get(position).et_document2.setVisibility(View.INVISIBLE);
		}

		// 다음 버튼에 대한 focus 설
		ResultDataArr.get(position).et_document1.setOnEditorActionListener(new OnEditorActionListener() {

			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				// TODO Auto-generated method stub
				if(ResultDataArr.get(pos).et_document1.isFocused())
				{
					if (actionId == EditorInfo.IME_ACTION_DONE && ResultDataArr.get(pos).document_count.equals("2")) {
						ResultDataArr.get(pos).et_document2.requestFocus();
					} else if (actionId == EditorInfo.IME_ACTION_NEXT && ResultDataArr.get(pos).document_count.equals("2")) {
						ResultDataArr.get(pos).et_document2.requestFocus();
					}
				}
				return false;
			}
		});
		
		ResultDataArr.get(position).et_document2.setOnEditorActionListener(new OnEditorActionListener() {
			
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				// TODO Auto-generated method stub
				if(ResultDataArr.get(pos).et_document2.isFocused())
				{
					if (actionId == EditorInfo.IME_ACTION_DONE) {
						if(getCount() > (pos+1))
						{
							ResultDataArr.get(pos+1).et_document1.requestFocus();
						}
					} else if (actionId == EditorInfo.IME_ACTION_NEXT) {
						if(getCount() > (pos+1))
						{
							ResultDataArr.get(pos+1).et_document1.requestFocus();
						}
					}
				}
				return false;
			}
		});

		ResultDataArr.get(position).et_document1.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				ResultDataArr.get(pos).document1 = s.toString();
			}
		});
		ResultDataArr.get(position).et_document2.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				ResultDataArr.get(pos).document2 = s.toString();
			}
		});
	

		return convertView;
	}
}
